// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   IOMessage.java

package ys8583.parse;

import com.solab.iso8583.common.ByteUtil;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ys8583.po.Field;
import ys8583.po.MessageConf;

// Referenced classes of package ys8583.parse:
//			Conf8583

public class IOMessage
{
	
	private final static String charset = "GBK";

	private static int BCD = 1;
	private static int ASCII = 2;
	private static int BINARY = 3;
	private static int HEX = 4;
	private static int HEXSTRING = 5;
	private static int LLVAR = 1;
	private static int LLLVAR = 2;
	private static int FIXLEN = 3;
	private Object reqMessageID;
	private Object rspMessageID;
	private Map reqMap;
	private Map rspMap;
	private Map reqHeadMap;
	private Map rspHeadMap;
	private Map confMap;
	private Conf8583 conf;
	private final String USABLE = "Y";
	private static int bytenum = 4096;
	private StringBuffer bitmapstr ;
	private byte[] iomessagebytes;

	public Map getReqHeadMap()
	{
		return reqHeadMap;
	}

	public void setReqHeadMap(Map reqHeadMap)
	{
		this.reqHeadMap = reqHeadMap;
	}

	public Map getReqMap()
	{
		return reqMap;
	}

	public void setReqMap(Map reqMap)
	{
		this.reqMap = reqMap;
	}

	public Map getRspHeadMap()
	{
		return rspHeadMap;
	}

	public void setRspHeadMap(Map rspHeadMap)
	{
		this.rspHeadMap = rspHeadMap;
	}

	public Map getRspMap()
	{
		return rspMap;
	}

	public void setRspMap(Map rspMap)
	{
		this.rspMap = rspMap;
	}

	public IOMessage(String item)
	{
		reqMap = new HashMap();
		rspMap = new HashMap();
		reqHeadMap = new HashMap();
		rspHeadMap = new HashMap();
		confMap = MessageConf.getInstance().getMap();
		conf = (Conf8583)confMap.get(item);
	}

	public IOMessage(String item, byte buf[])
		throws Exception
	{
		reqMap = new HashMap();
		rspMap = new HashMap();
		reqHeadMap = new HashMap();
		rspHeadMap = new HashMap();
		confMap = MessageConf.getInstance().getMap();
		conf = (Conf8583)confMap.get(item);
		iomessagebytes = buf;
		createMessage(buf);
	}

	public void createMessage(byte buf[])
		throws Exception
	{
		if (conf == null)
		{
			throw new Exception("--J8583-- createMessage, 8583conf is not exist ");
		} else
		{
			bitmapstr = new StringBuffer("\r\n");
			int headerLen = conf.getHeaderLen();
			byte headerBuf[];
			if(headerLen <=0){
				headerBuf = new byte[0];
			}else{
				headerBuf = ByteUtil.getsubByte(buf, 0, headerLen);
			}
			parseHeader(headerBuf);
			byte messageIDBuf[] = ByteUtil.getsubByte(buf, headerLen, buf.length - headerLen);
			int messageIDLen = parseMessageType(messageIDBuf);
			int bitmapLen = Integer.parseInt(conf.getBitmap()) / 8;
			byte bitmap[] = ByteUtil.getsubByte(buf, headerLen + messageIDLen, bitmapLen);
			bitmapstr.append("decode [bitmap]").append(ByteUtil.BinaryToHexString(bitmap)).append("\r\n");
			byte fieldBuf[] = ByteUtil.getsubByte(buf, headerLen + messageIDLen + bitmapLen, buf.length - (headerLen + messageIDLen + bitmapLen));
			parseBitmap(bitmap, fieldBuf);
			return;
		}
	}

	public void parseHeader(byte buf[])
		throws Exception
	{
		Map map = conf.getHeaderMap();
		Field f = null;
		int offset = 0;
		for (int i = 1; i <= map.size(); i++)
		{
			f = (Field)map.get(String.valueOf(i));
			if (f == null)
				throw new Exception((new StringBuilder("--J8583-- parseHeader: Config,head field ")).append(i).append(" is not exist ").toString());
			offset = setFieldValue(reqHeadMap, f, buf, offset, i);
		}

	}

	public int parseMessageType(byte buf[])
		throws Exception
	{
		Map map = conf.getTemplateMap();
		Field f = null;
		int offset = 0;
		f = (Field)map.get(String.valueOf(0));
		if (f == null)
		{
			throw new Exception("--J8583--  parseMessageType: Config,messageType  is not exist ");
		} else
		{
			offset = setFieldValue(reqMap, f, buf, offset, 0);
			reqMessageID = reqMap.get(new Integer(0));
			reqMap.remove(new Integer(0));
			bitmapstr.append("decode [0]").append(reqMessageID).append("\r\n");
			return offset;
		}
	}

	public void parseBitmap(byte buf[], byte fieldBuf[])
		throws Exception
	{
		BitSet bs = new BitSet(65);
		int pos = 1;
		for (int i = 0; i < 8; i++)
		{
			int bit = 128;
			for (int b = 0; b < 8; b++)
			{
				bs.set(pos++, (buf[i] & bit) != 0);
				bit >>= 1;
			}

		}

		if (bs.get(1))
		{
			for (int i = 8; i < 16; i++)
			{
				int bit = 128;
				for (int b = 0; b < 8; b++)
				{
					bs.set(pos++, (buf[i] & bit) != 0);
					bit >>= 1;
				}

			}

		}
		Map map = conf.getTemplateMap();
		Field f = null;
		int offset = 0;
		for (int i = 0; i <= Integer.parseInt(conf.getBitmap()); i++)
			if (i > 1)
			{
				bs.size();
				if (bs.get(i))
				{
					f = (Field)map.get(String.valueOf(i));
					if (f == null)
						throw new Exception((new StringBuilder("--J8583-- parseBitmap: Config,message field ")).append(i).append("   is not exist ").toString());
					offset = setFieldValue(reqMap, f, fieldBuf, offset, i);
					if("BINARY".equals(f.getType())){
						bitmapstr.append("decode [").append(i).append("]").append(ByteUtil.BinaryToHexString((byte[])getReqMesField(i))).append("\r\n");
					}else{
						bitmapstr.append("decode [").append(i).append("]").append(getReqMesField(i)).append("\r\n");
					}
				}
			}

	}

	public int setFieldValue(Map reqMap, Field f, byte fieldBuf[], int offset, int fieldIndex)
		throws Exception
	{
		int len = Integer.parseInt(f.getLength());
		byte valBuf[] = new byte[1000];
		int bufLen = 0;
		int subLen = 0;
		int parseInt[] = new int[2];
		switch (convertType(f.getType()))
		{
		default:
			break;

		case 1: // '\001'
			switch (convertType(f.getFormat()))
			{
			case 1: // '\001'
				parseInt = getParselens(f.getLenType(), "LLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = getBcdLen(parseInt[1]);
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.bcd2Str(valBuf, getBcdZerofillType(f.getFieldZeroFillType()), parseInt[1]));
				break;

			case 2: // '\002'
				parseInt = getParselens(f.getLenType(), "LLLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = getBcdLen(parseInt[1]);
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.bcd2Str(valBuf, getBcdZerofillType(f.getFieldZeroFillType()), parseInt[1]));
				break;

			case 3: // '\003'
				bufLen = getBcdLen(len);
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.bcd2Str(valBuf, getBcdZerofillType(f.getFieldZeroFillType()), len));
				break;
			}
			break;

		case 2: // '\002'
			switch (convertType(f.getFormat()))
			{
			case 1: // '\001'
				parseInt = getParselens(f.getLenType(), "LLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = parseInt[1];
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.toStringHex(ByteUtil.BinaryToHexString(valBuf)));
				break;

			case 2: // '\002'
				parseInt = getParselens(f.getLenType(), "LLLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = parseInt[1];
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.toStringHex(ByteUtil.BinaryToHexString(valBuf)));
				break;

			case 3: // '\003'
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, len);
				bufLen = len;
				reqMap.put(new Integer(fieldIndex), ByteUtil.toStringHex(ByteUtil.BinaryToHexString(valBuf)));
				break;
			}
			break;

		case 3: // '\003'
			bufLen = len / 8;
			valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
			reqMap.put(new Integer(fieldIndex), valBuf);
			break;

		case 4: // '\004'
			bufLen = len;
			valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
			reqMap.put(new Integer(fieldIndex), Integer.valueOf(Integer.parseInt(ByteUtil.bytesToHexString(valBuf), 16)));
			break;

		case 5: // '\005'
			switch (convertType(f.getFormat()))
			{
			case 1: // '\001'
				parseInt = getParselens(f.getLenType(), "LLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = parseInt[1];
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.BinaryToHexString(valBuf));
				break;

			case 2: // '\002'
				parseInt = getParselens(f.getLenType(), "LLLVAR", subLen, bufLen, fieldBuf, offset, len, parseInt);
				offset += parseInt[0];
				bufLen = parseInt[1];
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.BinaryToHexString(valBuf));
				break;

			case 3: // '\003'
				bufLen = len;
				valBuf = ByteUtil.getsubByte(fieldBuf, offset, bufLen);
				reqMap.put(new Integer(fieldIndex), ByteUtil.BinaryToHexString(valBuf));
				break;
			}
			break;
		}
		offset += bufLen;
		return offset;
	}

	private int getBcdLen(int len)
	{
		if (len % 2 != 0)
			return len / 2 + 1;
		else
			return len / 2;
	}

	private int[] getParselens(String lenType, String format, int subLen, int bufLen, byte fieldBuf[], int offset, int len, 
			int parseInt[])
		throws Exception
	{
		byte lenBuf[] = new byte[5];
label0:
		switch (convertType(lenType))
		{
		case 3: // '\003'
		default:
			break;

		case 1: // '\001'
			switch (convertType(format))
			{
			case 1: // '\001'
				subLen = 1;
				lenBuf = ByteUtil.getsubByte(fieldBuf, offset, subLen);
				bufLen = Integer.parseInt(ByteUtil.bcd2Str(lenBuf, getBcdZerofillType(conf.getLenZeroFillType()), 2));
				if (bufLen > len)
					throw new Exception((new StringBuilder("Field length ")).append(bufLen).append(" must be less than ").append(len).toString());
				break;

			case 2: // '\002'
				subLen = 2;
				lenBuf = ByteUtil.getsubByte(fieldBuf, offset, subLen);
				bufLen = Integer.parseInt(ByteUtil.bcd2Str(lenBuf, getBcdZerofillType(conf.getLenZeroFillType()), 3));
				if (bufLen > len)
					throw new Exception((new StringBuilder("Field length ")).append(bufLen).append(" must be less than ").append(len).toString());
				break;
			}
			break;

		case 2: // '\002'
			switch (convertType(format))
			{
			default:
				break label0;

			case 1: // '\001'
				subLen = 2;
				lenBuf = ByteUtil.getsubByte(fieldBuf, offset, subLen);
				bufLen = Integer.parseInt(ByteUtil.toStringHex(ByteUtil.BinaryToHexString(lenBuf)));
				if (bufLen > len)
					throw new Exception((new StringBuilder("Field length ")).append(bufLen).append(" must be less than ").append(len).toString());
				break label0;

			case 2: // '\002'
				subLen = 3;
				lenBuf = ByteUtil.getsubByte(fieldBuf, offset, subLen);
				bufLen = Integer.parseInt(ByteUtil.toStringHex(ByteUtil.BinaryToHexString(lenBuf)));
				break;
			}
			if (bufLen > len)
				throw new Exception((new StringBuilder("Field length ")).append(bufLen).append(" must be less than ").append(len).toString());
			break;
		}
		parseInt[0] = subLen;
		parseInt[1] = bufLen;
		return parseInt;
	}

	public Object getReqHeaderField(int index)
		throws Exception
	{
		if (index < 1 || index > reqHeadMap.size())
			throw new IndexOutOfBoundsException((new StringBuilder(" getReqMesField : Field index must be between 1 and ")).append(reqHeadMap.size()).toString());
		else
			return reqHeadMap.get(new Integer(index));
	}

	public Object getReqMesField(int index)
		throws Exception
	{
		if (index < 2 || index > 128)
			throw new IndexOutOfBoundsException(" getReqMesField : Field index must be between 2 and 128");
		else
			return reqMap.get(new Integer(index));
	}

	public int convertType(String type)
		throws Exception
	{
		if ("ASCII".equals(type))
			return ASCII;
		if ("BCD".equals(type))
			return BCD;
		if ("BINARY".equals(type))
			return BINARY;
		if ("HEX".equals(type))
			return HEX;
		if ("HEXSTRING".equals(type))
			return HEXSTRING;
		if ("LLVAR".equals(type))
			return LLVAR;
		if ("LLLVAR".equals(type))
			return LLLVAR;
		if ("FIXLEN".equals(type))
			return FIXLEN;
		else
			throw new Exception((new StringBuilder(String.valueOf(type))).append(" undefined ").toString());
	}

	public byte[] packHeader(int totalLen)
		throws Exception
	{
		byte val[] = new byte[bytenum];
		int vallen = 0;
		int len = rspHeadMap.size();
		if (len <= 0)
			return new byte[0];
//			throw new Exception(" packHeader , rspHeadMap is Empty ");
		Field f = null;
		Object value = null;
		byte field[] = (byte[])null;
		for (int i = 1; i <= len; i++)
			if (rspHeadMap.get(new Integer(i)) != null)
			{
				f = (Field)conf.getHeaderMap().get(String.valueOf(i));
				value = rspHeadMap.get(new Integer(i));
				if ("Y".equals(f.getIsTotalLen() != null ? ((Object) (f.getIsTotalLen())) : ""))
				{
					String tl = null;
					int length = 0;
					if ("HEX".equals(f.getType()))
					{
						tl = Integer.toHexString(totalLen);
						length = Integer.parseInt(f.getLength()) * 2;
					} else
					{
						tl = String.valueOf(totalLen);
						length = Integer.parseInt(f.getLength());
					}
					tl = ByteUtil.fillString(tl, '0', length, false);
					value = tl;
				}
				field = packField(f, value);
				if (field != null)
				{
					System.arraycopy(field, 0, val, vallen, field.length);
					vallen += field.length;
				}
			}

		byte newval[] = ByteUtil.getsubByte(val, 0, vallen);
		return newval;
	}

	public byte[] packMesType()
		throws Exception
	{
		if (rspMessageID == null)
		{
			throw new Exception(" packMesType , message type is Empty ");
		} else
		{
			Field f = null;
			f = (Field)conf.getTemplateMap().get(String.valueOf(0));
			byte val[] = packField(f, rspMessageID);
			bitmapstr.append(("encode [1]")).append(rspMessageID).append("\r\n");
			return val;
		}
	}

	public byte[] writeBitmap()
		throws Exception
	{
		byte val[] = new byte[bytenum];
		int vallen = 0;
		if (rspMap.size() <= 0)
			throw new Exception(" create bitmap , rspMap is Empty ");
		int flag[] = {
			128, 64, 32, 16, 8, 4, 2, 1
		};
		int bitmapLen = Integer.parseInt(conf.getBitmap());
		byte buf[] = new byte[bitmapLen / 8];
		Arrays.fill(buf, (byte)0);
		if (bitmapLen > 128)
			throw new Exception(" writeBitmap , bitmapLen is  larger than 128 ");
		if (bitmapLen > 64)
			rspMap.put(new Integer(1), "1");
		else
			rspMap.remove(new Integer(1));
		byte field[] = (byte[])null;
		Field f = null;
		Object value = null;
		for (int i = 1; i <= bitmapLen; i++)
			if (rspMap.get(new Integer(i)) != null)
			{
				buf[(i - 1) / 8] |= flag[(i - 1) % 8];
				if (i != 1)
				{
					f = (Field)conf.getTemplateMap().get(String.valueOf(i));
					value = rspMap.get(new Integer(i));
					field = packField(f, value);
					if("BINARY".equals(f.getType())){
						bitmapstr.append(("encode [")).append(i).append("]").append(ByteUtil.BinaryToHexString((byte[])value)).append("\r\n");
					}else{
						bitmapstr.append(("encode [")).append(i).append("]").append(value).append("\r\n");
					}
					if (field != null)
					{
						System.arraycopy(field, 0, val, vallen, field.length);
						vallen += field.length;
					}
				}
			}

		byte newval[] = ByteUtil.getsubByte(val, 0, vallen);
		
		return ByteUtil.byteAndByte(buf, newval);
	}

	public boolean isFieldUsed(String isUse)
	{
		return "Y".equals(isUse);
	}

	public byte[] packField(Field f, Object value)
		throws Exception
	{
		byte lenBuf[] = (byte[])null;
		byte valBuf[] = (byte[])null;
		byte newBuf[] = (byte[])null;
		int len = 0;
label0:
		switch (convertType(f.getType()))
		{
		default:
			break;

		case 1: // '\001'
			String b = (String)value;
			switch (convertType(f.getFormat()))
			{
			case 1: // '\001'
				len = b.getBytes(charset).length;
				if (len > 99)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 99").toString());
				lenBuf = setParselens(f.getLenType(), "LLVAR", len);
				valBuf = ByteUtil.str2Bcd(b, getBcdZerofillType(f.getFieldZeroFillType()));
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break;

			case 2: // '\002'
				len = b.getBytes(charset).length;
				if (len > 999)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 999").toString());
				lenBuf = setParselens(f.getLenType(), "LLLVAR", len);
				valBuf = ByteUtil.str2Bcd(b, getBcdZerofillType(f.getFieldZeroFillType()));
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break;

			case 3: // '\003'
				newBuf = ByteUtil.str2Bcd(b, getBcdZerofillType(f.getFieldZeroFillType()));
				break;
			}
			break;

		case 2: // '\002'
			String v = (String)value;
			switch (convertType(f.getFormat()))
			{
			default:
				break;

			case 1: // '\001'
				len = v.getBytes(charset).length;
				if (len > 99)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 99").toString());
				lenBuf = setParselens(f.getLenType(), "LLVAR", len);
				valBuf = v.getBytes(charset);
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break label0;

			case 2: // '\002'
				len = v.getBytes(charset).length;
				if (len > 999)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 999").toString());
				lenBuf = setParselens(f.getLenType(), "LLLVAR", len);
				valBuf = v.getBytes(charset);
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break;

			case 3: // '\003'
				newBuf = v.getBytes(charset);
				break;
			}
			break;

		case 3: // '\003'
			switch (convertType(f.getFormat()))
			{
			case 3: // '\003'
				newBuf = (byte[])value;
				break;
			}
			break;

		case 4: // '\004'
			String v1 = (String)value;
			switch (convertType(f.getFormat()))
			{
			case 3: // '\003'
				newBuf = ByteUtil.hexStringToBytes(v1);
				break;
			}
			break;

		case 5: // '\005'
			String v5 = (String)value;
			switch (convertType(f.getFormat()))
			{
			default:
				break;

			case 1: // '\001'
				len = v5.getBytes(charset).length / 2;
				if (len > 99)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 99").toString());
				lenBuf = setParselens(f.getLenType(), "LLVAR", len);
				valBuf = ByteUtil.hexStringToBytes(v5);
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break label0;

			case 2: // '\002'
				len =  v5.getBytes(charset).length / 2;
				if (len > 999)
					throw new Exception((new StringBuilder("Field length ")).append(len).append(" larger than 999").toString());
				lenBuf = setParselens(f.getLenType(), "LLLVAR", len);
				valBuf = ByteUtil.hexStringToBytes(v5);
				newBuf = ByteUtil.byteAndByte(lenBuf, valBuf);
				break;

			case 3: // '\003'
				newBuf = ByteUtil.hexStringToBytes(v5);
				break;
			}
			break;
		}
		return newBuf;
	}

	private byte[] setParselens(String lenType, String format, int bufLen)
		throws Exception
	{
		byte lenBuf[] = (byte[])null;
		switch (convertType(lenType))
		{
		case 3: // '\003'
		default:
			break;

		case 1: // '\001'
			switch (convertType(format))
			{
			case 1: // '\001'
				lenBuf = ByteUtil.str2Bcd(String.format("%02d", new Object[] {
					Integer.valueOf(bufLen)
				}), getBcdZerofillType(conf.getLenZeroFillType()));
				break;

			case 2: // '\002'
				lenBuf = ByteUtil.str2Bcd(String.format("%03d", new Object[] {
					Integer.valueOf(bufLen)
				}), getBcdZerofillType(conf.getLenZeroFillType()));
				break;
			}
			break;

		case 2: // '\002'
			switch (convertType(format))
			{
			case 1: // '\001'
				lenBuf = String.format("%02d", new Object[] {
					Integer.valueOf(bufLen)
				}).getBytes(charset);
				break;

			case 2: // '\002'
				lenBuf = String.format("%03d", new Object[] {
					Integer.valueOf(bufLen)
				}).getBytes(charset);
				break;
			}
			break;
		}
		return lenBuf;
	}

	public Object getReqMessageID()
	{
		return reqMessageID;
	}

	public void setReqMessageID(String reqMessageID)
	{
		this.reqMessageID = reqMessageID;
	}

	public Object getRspMessageID()
	{
		return rspMessageID;
	}

	public void setRspMessageID(String rspMessageID)
	{
		this.rspMessageID = rspMessageID;
	}

	public void setRspField(int index, Object value)
	{
		if (index < 2 || index > 128){
			throw new IndexOutOfBoundsException("Field index must be between 2 and 128");
		}
		rspMap.put(new Integer(index), value);
	}
	
	public Object getRspField(int index)
	{
		return rspMap.get(index);
	}

	public void removeField(int index)
	{
		if (index < 2 || index > 128)
		{
			throw new IndexOutOfBoundsException("Field index must be between 2 and 128");
		} else
		{
			rspMap.remove(new Integer(index));
			return;
		}
	}

	public void setRspHeaderField(int index, Object value)
	{
		if (index < 1)
		{
			throw new IndexOutOfBoundsException("Header Field index must be equal or greater than  1");
		} else
		{
			rspHeadMap.put(new Integer(index), value);
			return;
		}
	}

	public void setRspMesType(Object value)
	{
		rspMessageID = value;
	}

	public byte[] createResponseMsg()
		throws Exception
	{
		bitmapstr = new StringBuffer("\r\n");
		byte val[] = new byte[bytenum];
		int vallen = 0;
		int totalLen = 0;
		byte mesTypebuf[] = packMesType();
		byte bitvalbuf[] = writeBitmap();
		bitmapstr.append("encdoe [bitmap] ").append(ByteUtil.BinaryToHexString(bitvalbuf).substring(0, 32)).append("\r\n");
		totalLen = conf.getHeaderLen() + mesTypebuf.length + bitvalbuf.length;
		byte headerbuf[] = packHeader(totalLen);
		System.arraycopy(headerbuf, 0, val, vallen, headerbuf.length);
		vallen += headerbuf.length;
		System.arraycopy(mesTypebuf, 0, val, vallen, mesTypebuf.length);
		vallen += mesTypebuf.length;
		System.arraycopy(bitvalbuf, 0, val, vallen, bitvalbuf.length);
		byte newval[] = new byte[totalLen];
		newval = ByteUtil.getsubByte(val, 0, totalLen);
		iomessagebytes = newval;
		return newval;
	}

	private boolean getBcdZerofillType(String foramt)
		throws Exception
	{
		if ("L".equalsIgnoreCase(foramt))
			return true;
		if ("R".equalsIgnoreCase(foramt))
			return false;
		else
			throw new Exception(" BcdZerofillType index must be 'L' OR 'R' ");
	}

	public String toGetBitmapStr() {
		return bitmapstr.toString();
	}
	
	public byte[] toGetByte() {
		return iomessagebytes;
	}

}
